import type {
  GetSpuListApiResponse,
  GetSpuListParams,
  getAllSpuList,
  SaveOrUpdateSpuApiParams,
  SpuImageList,
  SpuSaleAttrList,
} from "./model/spuModel";
import request from "@/utils/request";

enum Api {
  getSpuListUrl = "/admin/product",
  getAllSpuListUrl = "/admin/product/baseSaleAttrList",
  saveSpuUrl = "/admin/product/saveSpuInfo",
  getSpuImageList = "/admin/product/spuImageList",
  getSaleAttrList = "/admin/product/spuSaleAttrList",
  updateSpuUrl = "/admin/product/updateSpuInfo",
}

export const getSpuListApi = ({
  page,
  limit,
  category3Id,
}: GetSpuListParams) => {
  return request.get<any, GetSpuListApiResponse>(
    Api.getSpuListUrl + `/${page}/${limit}`,
    {
      params: {
        category3Id,
      },
    }
  );
};

export const getAllSpuListApi = () => {
  return request.get<any, getAllSpuList>(Api.getAllSpuListUrl);
};

/**
 * 添加Spu
 * @param data SaveOrUpdateSpuApiParams
 * @returns null
 */
export const saveSpuApi = (data: SaveOrUpdateSpuApiParams) => {
  return request.post<any, null>(Api.saveSpuUrl, data);
};
/**
 * 获取图片列表
 * @param id
 * @returns SpuImageList
 */
export const getSpuImageListApi = (id: number) => {
  return request.get<any, SpuImageList>(Api.getSpuImageList + `/${id}`);
};

/**
 * 根据ID获取属性列表
 * @param id
 * @returns SpuSaleAttrList
 */
export const getSpuSaleAttrListApi = (id: number) => {
  return request.get<any, SpuSaleAttrList>(Api.getSaleAttrList + `/${id}`);
};

/**
 * 更新SPU
 * @param data SaveOrUpdateSpuApiParams
 * @returns null
 */
export const updateSpuApi = (data: SaveOrUpdateSpuApiParams) => {
  return request.post<any, null>(Api.updateSpuUrl, data);
};
